
#define ROVIO_CAM "http://143.215.111.166/Jpeg/CamImg[001].jpg"

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include "httpInterface.h"

#include "rovio.h"

using namespace cv;

// Create memory for calculations
static CvMemStorage* storage = 0;

// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;

using namespace cv;

// Function prototype for detecting and drawing an object from an image
void detect_and_draw( IplImage* image );

// Create a string that contains the cascade name
const char* cascade_name = "haarcascade_frontalface_alt.xml";


int main ( int argc, char **argv )
{
	VideoCapture camera(0); // video source for webcam
	camera.set(CV_CAP_PROP_FRAME_WIDTH,640);
	camera.set(CV_CAP_PROP_FRAME_HEIGHT,480);

	Mat camImage;
	IplImage image;
		
	while (true) {
 		http_fetch(ROVIO_CAM, "img.jpg");
	  	camImage = imread("img.jpg", 1);
	  	image = camImage;
	  	
	  	// Load the HaarClassifierCascade
		cascade = (CvHaarClassifierCascade*) cvLoad(cascade_name, 0, 0, 0);
		
		// Check whether the cascade has loaded successfully. Else report and error and 	quit
		if (!cascade) {
			fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
			return -1;
		}

		storage = cvCreateMemStorage(0);
		detect_and_draw(&image);
		
		if (cvWaitKey(10) >= 0) {
            break;
		}
	}

	cvDestroyAllWindows();
}

void detect_and_draw( IplImage* img )
{
	int scale = 1;
	
	// Create a new image based on the input image
	IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
	
	// Create two points to represent the face locations
	CvPoint pt1, pt2;
	int i;
	
	// Clear the memory storage which was used before
	cvClearMemStorage( storage );
	
	// Find whether the cascade is loaded, to find the faces. If yes, then:
	if( cascade )
	{
		// There can be more than one face in an image. So create a growable sequence of faces.
		// Detect the objects and store them in the sequence
		CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
										   1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
										   cvSize(40, 40) );
		
		if (faces) {
			for( i = 0; i < (faces ? faces->total : 0); i++ )
			{
				// Create a new rectangle for drawing the face
				CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
				
				// Find the dimensions of the face,and scale it if necessary
				pt1.x = r->x*scale;
				pt2.x = (r->x+r->width)*scale;
				pt1.y = r->y*scale;
				pt2.y = (r->y+r->height)*scale;
				
				// Draw the rectangle in the input image
				cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
			}
		}
	}
	
	// Show the image in the window named "result"
	cvShowImage( "FaceFinder", img );
	
	// Release the temp image created.
	cvReleaseImage( &temp );
}
